package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.MbrMemberScore;
import com.macro.mall.portal.service.MbrMemberScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * @author super
 * @title: CommonController
 * @description: TODO
 * @date 2020/8/15 14:16
 */
@Slf4j
@RestController
@RequestMapping("score")
@Api(tags = "score", description = "评分表(score)")
public class MbrMemberScoreController {

    @Autowired
    private MbrMemberScoreService mbrMemberScoreService;




    @ApiOperation("查询评价")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MbrMemberScore> list(@ApiParam("1-课程、2-视频、3-文章、4-服务订单、5-视频订单、6-讲师") @RequestParam(value = "targetType") Integer targetType,
                                             @ApiParam("如果是课程，对应的是课程id,如果是视频，对应的是视频ID") @RequestParam(value = "targetId") String targetId
    ) {
        MbrMemberScore one = mbrMemberScoreService.findOne(targetType, targetId);
        return CommonResult.success(one);
    }

    @ApiOperation("查询平均分")
    @RequestMapping(value = "/listAvg", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult listAvg(@ApiParam("专题ID") @RequestParam(value = "trainingId") Integer trainingId) {
        return CommonResult.success(mbrMemberScoreService.selectTrainingAvg(trainingId));
    }

    @ApiOperation("添加评分")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(Principal principal, @RequestBody MbrMemberScore request) {
        try {
            if(principal==null){
                return CommonResult.unauthorized(null);
            }
            mbrMemberScoreService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("专题评论最多的标签")
    @RequestMapping(value = "/tagMax", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult scoreMax(@RequestParam(value = "commonType") String commonType,
                                 @RequestParam(value = "trainingId") Integer trainingId,
                                 @RequestParam(value = "courseId",required = false) Integer courseId,
                                 @RequestParam(value = "num") Integer num
    ) {
        return CommonResult.success(mbrMemberScoreService.scoreMax(commonType,trainingId,courseId,num));
    }


}
